/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.xcommons;

import static cc.aileron.commons.utility.ResourceUtils.resource;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Properties;

import cc.aileron.commons.accessor.ConvertorNotFoundException;
import cc.aileron.commons.exception.ModuleConfigurationException;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.dao.db.DbConductDefault;
import cc.aileron.dao.db.DbConnectionManager;
import cc.aileron.dao.db.DbConnectionManagerSimple;
import cc.aileron.dao.db.DbDaoModule;
import cc.aileron.dao.db.logger.NullLogger;
import cc.aileron.dao.db.logger.StdoutLogger;
import cc.aileron.template.sql.impl.SqlLogger;

import com.google.inject.Module;

/**
 * DB-APPLICATION-MODULE
 * 
 * @author aileron
 * 
 */
public abstract class SimpleDbAppliactionModule implements Module
{
    /**
     * db接続モジュールの取得
     * 
     * @param conduct
     * @return
     * @throws ModuleConfigurationException
     * @throws ClassNotFoundException
     * @throws ResourceNotFoundException
     * @throws ConvertorNotFoundException
     * @throws SQLException
     * @throws IOException
     * @throws IllegalArgumentException
     * @throws SecurityException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    private DbDaoModule getDbDaoModule(
            final Class<? extends DbConductDefault> conduct)
            throws ModuleConfigurationException, ClassNotFoundException,
            ResourceNotFoundException, ConvertorNotFoundException,
            SQLException, IOException, IllegalArgumentException,
            SecurityException, InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException
    {

        final SqlLogger sqlLogger = Boolean.parseBoolean(properties
                .getProperty("db.debug")) ? new StdoutLogger()
                : new NullLogger();

        final DbConnectionManager connectionManager = new DbConnectionManagerSimple(
                properties);

        return new DbDaoModule(conduct.getConstructor(
                DbConnectionManager.class, SqlLogger.class).newInstance(
                connectionManager, sqlLogger));
    }

    /**
     * 設定ファイルの取得
     * 
     * @return
     * @throws IOException
     * @throws ResourceNotFoundException
     */
    private Properties getProperties(final String propertiesFilePath)
            throws IOException, ResourceNotFoundException
    {
        return resource(propertiesFilePath).toProperties();
    }

    /*
     * (非 Javadoc)
     * 
     * @see java.lang.Object#finalize()
     */
    @Override
    protected void finalize() throws Throwable
    {
        this.db.connectionManager.destroy();
    }

    /**
     * constractor
     * 
     * @param propertiesFilePath
     * @param conducts
     * 
     * @throws IOException
     * @throws ResourceNotFoundException
     * @throws ModuleConfigurationException
     * @throws ClassNotFoundException
     * @throws ConvertorNotFoundException
     * @throws SQLException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SecurityException
     * @throws IllegalArgumentException
     */
    public SimpleDbAppliactionModule(final String propertiesFilePath,
            final Class<? extends DbConductDefault> conducts)
            throws IOException, ResourceNotFoundException,
            ModuleConfigurationException, ClassNotFoundException,
            ConvertorNotFoundException, SQLException, IllegalArgumentException,
            SecurityException, InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException
    {
        this.properties = getProperties(propertiesFilePath);
        this.db = getDbDaoModule(conducts);
    }

    /**
     * properties
     */
    protected final Properties properties;

    /**
     * db-module
     */
    protected final DbDaoModule db;
}
